package org.server.config;


import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.Objects;


/**
 * @author Admin
 */
@Configuration
public class RedisConfig {

    static LettuceConnectionFactory redisConnectionFactory;
    static RedisTemplate<Object, Object> template;

    static {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName("localhost");
        redisStandaloneConfiguration.setPort(6379);
//        redisStandaloneConfiguration.setPassword("200307");
        redisConnectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration);
        template = new RedisTemplate<>();
        System.out.println("加载redis");
        redisConnectionFactory.afterPropertiesSet();
        RedisTemplate<Object, Object> template = RedisConfig.getTemplate();
        //连接工厂
        template.setConnectionFactory(redisConnectionFactory);
        FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
        // key采用String的序列化方式
        template.setKeySerializer(new StringRedisSerializer());

        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(new StringRedisSerializer());

        // value序列化方式采用jackson
        template.setValueSerializer(fastJsonRedisSerializer);

        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(fastJsonRedisSerializer);

        template.afterPropertiesSet();
        template.delete(Objects.requireNonNull(template.keys("*")));
    }

    public static RedisTemplate<Object, Object> getTemplate() {
        return template;
    }

    public static RedisConnectionFactory getRedisConnectionFactory() {
        return redisConnectionFactory;
    }

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        return RedisConfig.getRedisConnectionFactory();
    }

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory factory) {
//        System.out.println("加载redis");
//
//        RedisTemplate<Object, Object> template = RedisConfig.getTemplate();
//
//        //连接工厂
//        template.setConnectionFactory(factory);
//        FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
//        // key采用String的序列化方式
//        template.setKeySerializer(new StringRedisSerializer());
//
//        // hash的key也采用String的序列化方式
//        template.setHashKeySerializer(new StringRedisSerializer());
//
//        // value序列化方式采用jackson
//        template.setValueSerializer(fastJsonRedisSerializer);
//
//        // hash的value序列化方式采用jackson
//        template.setHashValueSerializer(fastJsonRedisSerializer);
//
//        template.afterPropertiesSet();
//        template.delete(Objects.requireNonNull(template.keys("*")));
        return template;
    }

    @Bean
    public HashOperations<Object, Object, Object> hashOperations(RedisTemplate<Object, Object> redisTemplate) {
        return redisTemplate.opsForHash();
    }
}
